import axios from 'axios'
import router from '@/router'
const instance = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 20000,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
let showError = (error) => {
    console.log(error)
}
instance.interceptors.request.use(
    (config) => {
        if (config.method === 'post' && config.headers['Content-Type'] != 'application/json') {
            let form = config.data
            let formData =
                config.headers['Content-Type'] === 'multipart/form-data' ? new FormData() : new URLSearchParams()
            for (let item in form) {
                if (form[item] === null) {
                    form[item] = ''
                }
                formData.append(item, form[item])
            }
            config.data = formData
        }
        return config
    },
    (error) => {
        showError('request:', error)
        return Promise.reject(error) // 在调用的那边可以拿到(catch)你想返回的错误信息
    }
)
instance.interceptors.response.use(
    function (response) {
        if (response.hasOwnProperty('code')) {
            if (response.data.code != '200') {
                throw new Error(response.data)
            }
        }
        return response.data
    },
    function (err) {
        if (err.response) {
            // 请求成功发出且服务器也响应了状态码，但状态代码超出了 2xx 的范围
            switch (err.response.status) {
                case 400:
                    err.message = '请求错误'
                    break
                case 401:
                    router.push({
                        path: '/login'
                    })
                    break
                case 403:
                    err.message = '拒绝访问'
                    break
                case 404:
                    err.message = `请求地址出错: ${err.response.config.url}`
                    break
                case 408:
                    err.message = '请求超时'
                    break
                case 500:
                    err.message = '服务器内部错误'
                    router.push({
                        path: `/500`
                    })
                    break
                case 501:
                    err.message = '服务未实现'
                    break
                case 502:
                    err.message = '网关错误'
                    break
                case 503:
                    err.message = '服务不可用'
                    router.push('/login')
                    /*global Message*/
                    Message.error('服务不可用,请稍等一下哦~~')
                    break
                case 504:
                    err.message = '网关超时'
                    break
                case 505:
                    err.message = 'HTTP版本不受支持'
                    break
                default:
                    showError(err.response)
            }
        } else if (err.request) {
            // 请求已经成功发起，但没有收到响应
            showError(err.request)
        } else {
            // 发送请求时出了点问题
            showError(err.message)
        }
        return Promise.reject(err)
    }
)
export const get = (url, params) => {
    return instance.request({
        url,
        method: 'get',
        params
    })
}
export const post = (url, data, containfile) => {
    return instance.request({
        url,
        method: 'post',
        data,
        headers: { 'content-type': containfile ? 'multipart/form-data' : 'application/x-www-form-urlencoded' }
    })
}
export const postJson = (url, data) => {
    return instance.request({
        url,
        method: 'post',
        data,
        headers: { 'content-type': 'application/json' }
    })
}
export const init = (showErrorFunction) => {
    showError = showErrorFunction
}
